package com.xc.freeapp.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

public class DateUtils {
	private static  SimpleDateFormat sdf ;
	private static Logger log = Logger.getLogger(DateUtils.class.getClass());
	private static  SimpleDateFormat sdfweek =new SimpleDateFormat("EEE");
	public static String getWeekDay(Date date){
		 	String[] weeks = {"周日","周一","周二","周三","周四","周五","周六"};  
	        Calendar cal = Calendar.getInstance();  
	        cal.setTime(date);  
	        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;  
	        if(week_index<0){  
	            week_index = 0;  
	        }   
	        return weeks[week_index];  
	}

	public static String formatDateToString(Date date,String format) {
		sdf = new SimpleDateFormat(format);
		if (null != date) {
			return sdf.format(date);
		} else {
			return null;
		}
	}


	public static Date stringToDate(String dateSting,String format){
		Date resultDate =null;
		sdf = new SimpleDateFormat(format);
		try {
			resultDate = sdf.parse(dateSting);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			log.error("格式化日期出错:"+dateSting);
		}finally{
			return resultDate;
		}
		
	}

	public static String formatDateToString(Date date) {
		sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (null != date) {
			return sdf.format(date);
		} else {
			new Exception("date format error");
			return null;
		}
	}

	public static Date formatDateToDate(Date date) {
		Date formatDate = null;
		sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			if (null != date) {
				formatDate = sdf.parse(sdf.format(date));
				return formatDate;
			} else {
				new Exception("date format error");
				return null;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return formatDate;
	}
	public static Date getDate(){
		return new Date();
	}
	

}
